<template>
	<view>
		<u--form labelPosition="left" labelWidth="80" ref="uForm">
			<u-form-item label="头像" ref="item1">
				<template>
					<u-upload 
					:fileList="fileList1" 
					@afterRead="afterRead" 
					@delete="deletePic" 
					name="1" 
					multiple
					:maxCount="1"></u-upload>
				</template>
			</u-form-item>
		
		
			<u-form-item label="昵称"  ref="item1">
					<u--input placeholder="输入昵称" v-model="nickname" ></u--input>
			</u-form-item>
			<view class="button-container">
			<u-button class="but"  @click="UpdateUserInfo">确认</u-button>
			<u-button class="but"  @click="ret">取消</u-button>
			</view>
		</u--form>
	</view>
</template>


<script>
	import {
		uploadImg
	} from '@/utils/api/public.js';
	import {
		updateUserInfo
	} from '@/utils/api/user_info.js';
	export default {
		data() {
			return {
				image: '',
				nickname: '',
				fileList1: [],
			}
		},
		methods: {
			async toPage(url) {
				this.$tools.toPage(url)
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			async afterRead(event) {
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				let item = this[`fileList${event.name}`][fileListLen]
				this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					url: lists[0].url
				}))
				this.image=lists[0].url
			},
			async UpdateUserInfo() {
				if (this.image != '') {
					await uploadImg(this.image).then(res => {
						if (res.data.code != 0) {
							return
						}
						
						this.image = res.data.data
						
					})
				}
				
				await updateUserInfo({
					'nickname': this.nickname,
					'avatar': this.image
				}).then(res => {
					if (res.data.code != 0) {
						return
					}
					this.ret()
					return;
				})
			},
			ret(){
				console.log(111)
				uni.redirectTo({
					url: 'userinfo',
				});
			}
			
		}

	}
</script>

<style>
	.label {
		width: 80px;
		font-size: 16px;
		text-align: right;
		margin-right: 10px;
	}
	
	.button-container {
	  display: flex; 
	  justify-content: space-between; 
	  align-items: center; 
	  margin-top: 20px; 
	}  
	
	.but {
		background-color: deepskyblue;
		height: 30px;
		line-height: 30px;
		border: none;
		color: white;
		padding: 0 15px;
		border-radius: 4px;
		cursor: pointer;
		margin-top: 20rpx;
		width: 150rpx;
	}
</style>